package edu.wit.common.web;

import com.fasterxml.jackson.annotation.JsonView;
import edu.wit.common.constant.CommonConstant;
import edu.wit.common.exception.ResultEnums;
import edu.wit.common.utils.ConvertUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;
import java.util.Date;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 5492290939123524840L;

    @JsonView(GeneralViews.errorView.class)
    private int code = ResultEnums.SUCCESS.getCode();

    @JsonView(GeneralViews.errorView.class)
    private String msg = ResultEnums.SUCCESS.getMsg();

    @JsonView(GeneralViews.normalView.class)
    private T data;
    
    @JsonView(GeneralViews.normalView.class)
    private Date nowTime;

    public Result(T data) {
        super();
        this.data = data;
    }

    public Result(int code, String msg) {
        super();
        this.code = code;
        this.msg = msg;
    }

    public static <T> Result<T> success(T model, String msg) {
        return succeedWith(model, ResultEnums.SUCCESS.getCode(), msg);
    }

    public static <T> Result<T> success(T model) {
        return succeedWith(model, ResultEnums.SUCCESS.getCode(), ResultEnums.SUCCESS.getMsg());
    }

    public static <T> Result<T> success() {
        return success(ResultEnums.SUCCESS.getMsg());
    }

    public static <T> Result<T> success(String msg) {
        return success(null, msg);
    }

    public static <T> Result<T> succeedWith(T datas, Integer code, String msg) {
        return new Result<T>(code, msg, datas, new Date());
    }
    public static <T> Result<T> succeedWith(T datas, Integer code, String msg, Date date) {
    	return new Result<T>(code, msg, datas, date);
    }

    public static <T> Result<T> failed(String msg) {
        return failedWith(null, ResultEnums.SYSTEM_ERROR.getCode(), msg);
    }

    public static <T> Result<T> failed(T model, String msg) {
        return failedWith(model, ResultEnums.SYSTEM_ERROR.getCode(), msg);
    }

    public static <T> Result<T> failed(ResultEnums resultEnums) {
        return failedWith(null, resultEnums.getCode(), resultEnums.getMsg());
    }

    public static <T> Result<T> failed(Integer code, String msg) {
        return failedWith(null, code, msg);
    }

    public static <T> Result<T> failedWith(T datas, Integer code, String msg) {
        return new Result<T>(code, msg, datas, new Date());
    }
    public static <T> Result<T> failedWith(T datas, Integer code, String msg, Date date) {
    	return new Result<T>(code, msg, datas, date);
    }

    @SuppressWarnings("rawtypes")
	public static void resultJsonError(HttpServletResponse response,Result r) throws IOException {
        response.setStatus(r.getCode());
        response.setContentType(CommonConstant.CONTENT_TYPE);
        response.getWriter().write(ConvertUtil.getJson(r));
        response.getWriter().flush();
        response.getWriter().close();
    }

}
